const express = require('express')
const router = express.Router()
const Address = require('../db/address')
const jwt = require('jsonwebtoken')
//新增收货地址
router.post('/api/addAddress', (req, res) => {
  let token = req.headers.authorization
  new Promise((resolved, rejected) => {
    resolved(jwt.verify(token, 'jwt'))
  }).then(val => {
    let {
      name,
      tel,
      province,
      city,
      county,
      addressDetail,
      isDefault,
      areaCode
    } = req.body
    let uId = val.tel
    //查询用户收货地址
    Address.find({
      uId
    }).then(data => {
      //判断是否有收货地址
      if (data.length) {
        //本次是否为默认地址
        isDefault = JSON.parse(isDefault)
        if (isDefault) {
          //本次是默认收货地址,需要把之前的默认地址去掉
          Address.updateMany({
            uId
          }, {
            isDefault: false
          }).then(data => {
            //然后添加新的默认地址
            Address.create({
              uId,
              name,
              tel,
              province,
              city,
              county,
              addressDetail,
              isDefault,
              areaCode
            }).then(data => {
              return res.send({
                code: 200,
                success: true,
                msg: '收货地址添加成功'
              })
            })
          })
        } else {
          //不是默认地址直接添加
          Address.create({
            uId,
            name,
            tel,
            province,
            city,
            county,
            addressDetail,
            isDefault,
            areaCode
          }).then(data => {
            return res.send({
              code: 200,
              success: true,
              msg: '收货地址添加成功'
            })
          })
        }
      } else {
        //没有直接添加
        Address.create({
          uId,
          name,
          tel,
          province,
          city,
          county,
          addressDetail,
          isDefault,
          areaCode
        }).then(data => {
          return res.send({
            code: 200,
            success: true,
            msg: '收货地址添加成功'
          })
        })
      }
    })
  }).catch(err => {
    return res.send({
      code: 400,
      msg: err.message
    })
  })
})
//查询收货地址
router.post('/api/selectAddress', (req, res) => {
  let token = req.headers.authorization
  new Promise((resolved, rejected) => {
    resolved(jwt.verify(token, 'jwt'))
  }).then(val => {
    let uId = val.tel
    Address.find({
      uId
    }).then(data => {
      return res.send({
        code: 200,
        success: true,
        data
      })
    })
  }).catch(err => {
    return res.send({
      code: 400,
      msg: err.message
    })
  })
})
//编辑收货地址
router.post('/api/updateAddress', (req, res) => {
  let token = req.headers.authorization
  new Promise((resolved, rejected) => {
    resolved(jwt.verify(token, 'jwt'))
  }).then(val => {
    let uId = val.tel
    let {
      _id,
      name,
      tel,
      province,
      city,
      county,
      addressDetail,
      isDefault,
      areaCode
    } = req.body
    isDefault = JSON.parse(isDefault)
    //本次修改是否为默认地址
    if (isDefault) {
      //默认地址,把该用户之前的默认地址取消
      Address.updateMany({uId}, {
        isDefault: false
      }).then(data => {
        //再修改当前地址信息
        Address.updateMany({
          _id
        }, {
          name,
          tel,
          province,
          city,
          county,
          addressDetail,
          isDefault,
          areaCode
        }).then(data => {
          return res.send({
            code: 200,
            success: true,
            msg: '地址修改成功',
            data
          })
        })
      })
    } else {
      //不是默认地址,直接修改
      Address.updateMany({
        _id
      }, {
        name,
        tel,
        province,
        city,
        county,
        addressDetail,
        isDefault,
        areaCode
      }).then(data => {
        return res.send({
          code: 200,
          success: true,
          msg: '地址修改成功',
          data
        })
      })
    }
  }).catch(err => {
    return res.send({
      code: 400,
      msg: err.message
    })
  })
})
//删除收货地址
router.post('/api/deleteAddress', (req, res) => {
    let token = req.headers.authorization
    new Promise((resolved, rejected) => {
      resolved(jwt.verify(token, 'jwt'))
    }).then(val => {
      let {_id} = req.body
      Address.deleteMany({_id}).then(data=>{
        return res.send({
            code:200,
            success:true,
            msg:'地址删除成功',
            data
        })
      })
    }).catch(err => {
      return res.send({
        code: 400,
        msg: err.message
      })
    })
  })
module.exports = router
